﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace SpriteEditor
{
    public class Sprite : SpriteCalculation
    {
        public string sprite_FileName { get; set; }

        public string spr_image_FileName { get; set; }
        public Bitmap spr_image { get; set; }

        public List<Module> lst_Modules { get; set; }
        public List<Frame> lst_Frames { get; set; }
        public List<Animation> lst_Animations { get; set; }

        public Sprite()
        {
            lst_Modules = new List<Module>();
            lst_Frames = new List<Frame>();
            lst_Animations = new List<Animation>();
        }
        //---------------------------------------
        //Functions of list of mudules
        //---------------------------------------
        public Module getModuleFromID(int id)
        {
            if (lst_Modules != null)
            {
                for (int i = 0; i < lst_Modules.Count; i++)
                {
                    if (lst_Modules[i].ID == id)
                        return lst_Modules[i];
                }
            }
            return null;
        }

        public int getModuleIndexFromID(int id)
        {
            if (lst_Modules != null)
            {
                for (int i = 0; i < lst_Modules.Count; i++)
                {
                    if (lst_Modules[i].ID == id)
                        return i;
                }
            }
            return -1;
        }

        public Module getModuleFromPosition(Point p_vt)
        {
            if (lst_Modules != null)
            {
                for (int i = 0; i < lst_Modules.Count; i++)
                {
                    if (IsPointInRect(p_vt, new Rectangle(lst_Modules[i].X, lst_Modules[i].Y,
                                             lst_Modules[i].W, lst_Modules[i].H)))
                    {
                        return lst_Modules[i];
                    }
                }
            }
            return null;
        }

        public int getModuleIndexFromPosition(Point p_vt)
        {
            if (lst_Modules != null)
            {
                for (int i = 0; i < lst_Modules.Count; i++)
                {
                    if (IsPointInRect(p_vt, new Rectangle(lst_Modules[i].X, lst_Modules[i].Y,
                                             lst_Modules[i].W, lst_Modules[i].H)))
                    {
                        return i;
                    }
                }
            }
            return -1;
        }

        public int getFreeModuleId()
        {
            List<int> arl = new List<int>();
            if (lst_Modules != null)
            {
                for (int i = 0; i < lst_Modules.Count; i++)
                    arl.Add(lst_Modules[i].ID);
            }

            if (lst_Frames != null)
            {
                for (int i = 0; i < lst_Frames.Count; i++)
                {
                    if (lst_Frames[i].lst_ModulesOfFrame != null)
                    {
                        for (int j = 0; j < lst_Frames[i].lst_ModulesOfFrame.Count; j++)
                        {
                            arl.Add(lst_Frames[i].lst_ModulesOfFrame[j].ID);
                        }
                    }
                }
            }
            arl.Sort();

            if (arl == null || arl.Count == 0 || arl[0] != 1000)
            {
                return 1000;
            }
            else
            {
                for (int i = 1; i < arl.Count; i++)
                {
                    if (arl[i] > arl[i - 1] + 1)
                        return arl[i - 1] + 1;
                }
            }
            return arl[arl.Count - 1] + 1;
        }

        public void newModule()
        {
            if (spr_image != null)
            {
                if (lst_Modules == null)
                    lst_Modules = new List<Module>();

                int newID = getFreeModuleId();
                lst_Modules.Add(new Module()
                {
                    Index = lst_Modules.Count,
                    ID = newID,
                    X = 0,
                    Y = 0,
                    H = 32,
                    W = 32,
                    Module_Type = "IMAGE",
                    Module_Color = "FF000000",
                    Desc = ""
                });
            }
        }

        public void removeModuleAtIndex(int curIndex)
        {
            if (lst_Modules != null && lst_Modules.Count > curIndex)
            {
                lst_Modules.RemoveAt(curIndex);

                if (lst_Modules.Count > 0)
                {
                    for (int i = 0; i < lst_Modules.Count; i++)
                        lst_Modules[i].Index = i;
                }
            }
        }

        public void removeModuleAtID(int curID)
        {
            if (lst_Modules != null && lst_Modules.Count > 0)
            {
                int remIndex = -1;
                for (int i = 0; i < lst_Modules.Count; i++)
                {
                    if (lst_Modules[i].ID == curID)
                    {
                        remIndex = i;
                        break;
                    }
                }

                if (remIndex >= 0 && remIndex < lst_Modules.Count)
                    removeModuleAtIndex(remIndex);
            }
        }

        public void cloneModuleAtIndex(int curIndex)
        {
            if (lst_Modules != null && lst_Modules.Count > curIndex)
            {
                Module newModule = lst_Modules[curIndex].clone();
                newModule.ID = getFreeModuleId();

                lst_Modules.Insert(curIndex + 1, newModule);

                for (int i = curIndex + 1; i < lst_Modules.Count; i++)
                {
                    lst_Modules[i].Index += 1;
                }
            }
        }

        public void upModuleAtIndex(int curIndex)
        {
            if (lst_Modules != null && curIndex > 0 
                && lst_Modules.Count > curIndex)
            {
                Module temp = lst_Modules[curIndex];
                lst_Modules[curIndex] = lst_Modules[curIndex - 1];
                lst_Modules[curIndex - 1] = temp;

                lst_Modules[curIndex].Index++;
                lst_Modules[curIndex - 1].Index--;
            }
        }

        public void downModuleAtIndex(int curIndex)
        {
            if (lst_Modules != null && curIndex >= 0
                   && lst_Modules.Count - 1 > curIndex)
            {
                Module temp = lst_Modules[curIndex];
                lst_Modules[curIndex] = lst_Modules[curIndex + 1];
                lst_Modules[curIndex + 1] = temp;

                lst_Modules[curIndex].Index--;
                lst_Modules[curIndex + 1].Index++;
            }
        }

        public void setModuleTypeAtIndex(int curIndex, int newType)
        {
            if (lst_Modules != null
                && lst_Modules.Count > curIndex)
            {
                lst_Modules[curIndex].setType(newType);

                if (newType == XmlEngine.TYPE_FILL_RECT
                    || newType == XmlEngine.TYPE_RECT)
                {
                    if (lst_Modules[curIndex].Module_Color == null
                        || lst_Modules[curIndex].Module_Color == "")
                        lst_Modules[curIndex].Module_Color = "FFFF00FF";
                }
                else if (newType == XmlEngine.TYPE_IMAGE)
                {
                    lst_Modules[curIndex].Module_Color = "";
                }
            }
        }

        //---------------------------------------
        //Functions of list of frames
        //---------------------------------------
        public int getModuleOfFrameIndexFromPosition(Point p_vt, int frameIndex)
        {
            if (lst_Frames != null && lst_Frames.Count > frameIndex
                && lst_Frames[frameIndex] != null && lst_Frames[frameIndex].lst_ModulesOfFrame != null)
            {
                //for (int i = 0; i < lst_Frames[frameIndex].lst_ModulesOfFrame.Count; i++)
                for (int i = lst_Frames[frameIndex].lst_ModulesOfFrame.Count - 1; i >= 0; i--)
                {
                    ModulesOfFrame tmp_ModuleOfFrame = lst_Frames[frameIndex].lst_ModulesOfFrame[i];
                    Module tmp_Module = getModuleFromID(lst_Frames[frameIndex].lst_ModulesOfFrame[i].ID);

                    if (tmp_Module != null)
                    {
                        if (IsPointInRect(p_vt, new Rectangle(tmp_ModuleOfFrame.OX, tmp_ModuleOfFrame.OY, tmp_Module.W, tmp_Module.H)))
                        {
                            return i;
                        }
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
            return -1;
        }

        public Frame getFrameFromID(int id)
        {
            if (lst_Frames != null)
            {
                for (int i = 0; i < lst_Frames.Count; i++)
                {
                    if (lst_Frames[i].ID == id)
                        return lst_Frames[i];
                }
            }
            return null;
        }

        public int getFrameIndexFromID(int id)
        {
            if (lst_Frames != null)
            {
                for (int i = 0; i < lst_Frames.Count; i++)
                {
                    if (lst_Frames[i].ID == id)
                        return i;
                }
            }
            return -1;
        }

        public int getFreeFrameId()
        {
            List<int> arl = new List<int>();
            if (lst_Frames != null)
            {
                for (int i = 0; i < lst_Frames.Count; i++)
                    arl.Add(lst_Frames[i].ID);
            }

            if (lst_Animations != null)
            {
                for (int i = 0; i < lst_Animations.Count; i++)
                {
                    if (lst_Animations[i].lst_FramesOfAnimation != null)
                    {
                        for (int j = 0; j < lst_Animations[i].lst_FramesOfAnimation.Count; j++)
                        {
                            arl.Add(lst_Animations[i].lst_FramesOfAnimation[j].ID);
                        }
                    }
                }
            }
            arl.Sort();

            if (arl == null || arl.Count == 0 || arl[0] != 2000)
            {
                return 2000;
            }
            else
            {
                for (int i = 1; i < arl.Count; i++)
                {
                    if (arl[i] > arl[i - 1] + 1)
                        return arl[i - 1] + 1;
                }
            }
            return arl[arl.Count - 1] + 1;
        }

        public void newFrame()
        {
            if (spr_image != null && lst_Modules != null)
            {
                if (lst_Frames == null)
                    lst_Frames = new List<Frame>();

                int newID = getFreeFrameId();
                lst_Frames.Add(new Frame()
                {
                    Index = lst_Modules.Count,
                    ID = newID,
                    X = 0,
                    Y = 0,
                    W = 0,
                    H = 0,
                    Desc = "",
                    NumOfModules = 0,
                    lst_ModulesOfFrame = null
                });
            }
        }

        public void removeFrameAtIndex(int curIndex)
        {
            if (lst_Frames != null && lst_Frames.Count > curIndex)
            {
                lst_Frames.RemoveAt(curIndex);

                if (lst_Frames.Count > 0)
                {
                    for (int i = 0; i < lst_Frames.Count; i++)
                        lst_Frames[i].Index = i;
                }
            }
        }

        public void removeFrameAtID(int curID)
        {
            if (lst_Frames != null && lst_Frames.Count > 0)
            {
                int remIndex = -1;
                for (int i = 0; i < lst_Frames.Count; i++)
                {
                    if (lst_Frames[i].ID == curID)
                    {
                        remIndex = i;
                        break;
                    }
                }

                if (remIndex >= 0 && remIndex < lst_Modules.Count)
                    removeFrameAtIndex(remIndex);
            }
        }

        public void cloneFrameAtIndex(int curIndex)
        {
            if (lst_Frames != null && lst_Frames.Count > curIndex)
            {
                Frame newFrame = lst_Frames[curIndex].clone();
                newFrame.ID = getFreeFrameId();

                lst_Frames.Insert(curIndex + 1, newFrame);

                for (int i = curIndex + 1; i < lst_Frames.Count; i++)
                {
                    lst_Frames[i].Index += 1;
                }
            }
        }

        public void upFrameAtIndex(int curIndex)
        {
            if (lst_Frames != null && curIndex > 0
                && lst_Frames.Count > curIndex)
            {
                Frame temp = lst_Frames[curIndex];
                lst_Frames[curIndex] = lst_Frames[curIndex - 1];
                lst_Frames[curIndex - 1] = temp;

                lst_Frames[curIndex].Index++;
                lst_Frames[curIndex - 1].Index--;
            }
        }

        public void downFrameAtIndex(int curIndex)
        {
            if (lst_Frames != null && curIndex >= 0
                   && lst_Frames.Count - 1 > curIndex)
            {
                Frame temp = lst_Frames[curIndex];
                lst_Frames[curIndex] = lst_Frames[curIndex + 1];
                lst_Frames[curIndex + 1] = temp;

                lst_Frames[curIndex].Index--;
                lst_Frames[curIndex + 1].Index++;
            }
        }

        public void newModuleOfFrames(int curFrame, int newModuleIndex)
        {
            if (lst_Frames != null && curFrame >= 0
                && lst_Frames.Count > curFrame)
            {
                if (lst_Frames[curFrame].lst_ModulesOfFrame == null)
                    lst_Frames[curFrame].lst_ModulesOfFrame = new List<ModulesOfFrame>();

                ModulesOfFrame newModuleOfFrames = new ModulesOfFrame()
                    {
                        Index = lst_Frames[curFrame].lst_ModulesOfFrame.Count,
                        ID = lst_Modules[newModuleIndex].ID,
                        OX = 0,
                        OY = 0
                    };

                lst_Frames[curFrame].lst_ModulesOfFrame.Add(newModuleOfFrames);
                lst_Frames[curFrame].NumOfModules++;
                lst_Frames[curFrame].GenerateImage();
            }
        }

        public void deleteModuleOfFrames(int curFrame, int curMOFrames)
        {
            if (lst_Frames != null && curFrame >= 0 && curMOFrames >= 0
                && lst_Frames.Count > curFrame
                && lst_Frames[curFrame].lst_ModulesOfFrame != null
                && lst_Frames[curFrame].lst_ModulesOfFrame.Count > curMOFrames)
            {
                lst_Frames[curFrame].lst_ModulesOfFrame.RemoveAt(curMOFrames);
                lst_Frames[curFrame].NumOfModules--;

                if (lst_Frames[curFrame].lst_ModulesOfFrame.Count > 0)
                {
                    for (int i = 0; i < lst_Frames[curFrame].lst_ModulesOfFrame.Count; i++)
                        lst_Frames[curFrame].lst_ModulesOfFrame[i].Index = i;
                }
                lst_Frames[curFrame].GenerateImage();
            }
        }

        public void cloneModuleOfFrames(int curFrame, int curMOFrames)
        {
            if (lst_Frames != null && curFrame >= 0 && curMOFrames >= 0
                && lst_Frames.Count > curFrame
                && lst_Frames[curFrame].lst_ModulesOfFrame != null
                && lst_Frames[curFrame].lst_ModulesOfFrame.Count > curMOFrames)
            {
                ModulesOfFrame newModuleOfFrames = lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames].clone();
                lst_Frames[curFrame].lst_ModulesOfFrame.Insert(curMOFrames, newModuleOfFrames);
                lst_Frames[curFrame].NumOfModules++;

                for (int i = curMOFrames + 1; i < lst_Frames[curFrame].lst_ModulesOfFrame.Count; i++)
                {
                    lst_Frames[curFrame].lst_ModulesOfFrame[i].Index = i;
                }
                lst_Frames[curFrame].GenerateImage();
            }
        }

        public void upModuleOfFramesAtIndex(int curFrame, int curMOFrames)
        {
            if (lst_Frames != null && curFrame >= 0 && curMOFrames > 0
                && lst_Frames.Count > curFrame
                && lst_Frames[curFrame].lst_ModulesOfFrame != null
                && lst_Frames[curFrame].lst_ModulesOfFrame.Count > curMOFrames)
            {
                ModulesOfFrame tmp_mof = lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames];
                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames] = lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames - 1];
                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames - 1] = tmp_mof;

                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames].Index++;
                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames - 1].Index--;
                lst_Frames[curFrame].GenerateImage();
            }
        }

        public void downModuleOfFramesAtIndex(int curFrame, int curMOFrames)
        {
            if (lst_Frames != null && curFrame >= 0 && curMOFrames >= 0
                && lst_Frames.Count > curFrame
                && lst_Frames[curFrame].lst_ModulesOfFrame != null
                && lst_Frames[curFrame].lst_ModulesOfFrame.Count - 1 > curMOFrames)
            {
                ModulesOfFrame tmp_mof = lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames];
                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames] = lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames + 1];
                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames + 1] = tmp_mof;

                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames].Index--;
                lst_Frames[curFrame].lst_ModulesOfFrame[curMOFrames + 1].Index++;
                lst_Frames[curFrame].GenerateImage();
            }
        }

        //---------------------------------------
        //Functions of list of animations
        //---------------------------------------
        //public int getFrameOfAnimationIndexFromPosition(Point p_vt, int animationIndex)
        //{
        //    if (lst_Animations != null && lst_Animations.Count > animationIndex
        //        && lst_Animations[animationIndex] != null && lst_Animations[animationIndex].lst_FramesOfAnimation != null)
        //    {
        //        //for (int i = 0; i < lst_Animations[frameIndex].lst_FramesOfAnimation.Count; i++)
        //        for (int i = lst_Animations[animationIndex].lst_FramesOfAnimation.Count - 1; i >= 0; i--)
        //        {
        //            FramesOfAnimation tmp_FrameOfAnimation = lst_Animations[animationIndex].lst_FramesOfAnimation[i];
        //            Frame tmp_Frame = getFrameFromID(lst_Animations[animationIndex].lst_FramesOfAnimation[i].ID);

        //            if (tmp_Frame != null)
        //            {
        //                if (IsPointInRect(p_vt, new Rectangle(tmp_FrameOfAnimation.OX, tmp_FrameOfAnimation.OY, tmp_Frame.W, tmp_Frame.H)))
        //                {
        //                    return i;
        //                }
        //            }
        //            else
        //            {
        //                return -1;
        //            }
        //        }
        //    }
        //    return -1;
        //}

        public int getFreeAnimId()
        {
            List<int> arl = new List<int>();
            if (lst_Animations != null)
            {
                for (int i = 0; i < lst_Animations.Count; i++)
                    arl.Add(lst_Animations[i].ID);
            }
            arl.Sort();

            if (arl == null || arl.Count == 0 || arl[0] != 3000)
            {
                return 3000;
            }
            else
            {
                for (int i = 1; i < arl.Count; i++)
                {
                    if (arl[i] > arl[i - 1] + 1)
                        return arl[i - 1] + 1;
                }
            }
            return arl[arl.Count - 1] + 1;
        }

        public void newAnimation()
        {
            if (spr_image != null && lst_Modules != null && lst_Frames != null)
            {
                if (lst_Animations == null)
                    lst_Animations = new List<Animation>();

                int newID = getFreeAnimId();
                lst_Animations.Add(new Animation()
                {
                    Index = lst_Animations.Count,
                    ID = newID,
                    NumOfFrames = 0,
                    Desc = "",
                    lst_FramesOfAnimation = null
                });
            }
        }

        public void removeAnimationAtIndex(int curIndex)
        {
            if (lst_Animations != null && lst_Animations.Count > curIndex)
            {
                lst_Animations.RemoveAt(curIndex);

                if (lst_Animations.Count > 0)
                {
                    for (int i = 0; i < lst_Animations.Count; i++)
                        lst_Animations[i].Index = i;
                }
            }
        }

        public void removeAnimationAtID(int curID)
        {
            if (lst_Animations != null && lst_Animations.Count > 0)
            {
                int remIndex = -1;
                for (int i = 0; i < lst_Animations.Count; i++)
                {
                    if (lst_Animations[i].ID == curID)
                    {
                        remIndex = i;
                        break;
                    }
                }

                if (remIndex >= 0 && remIndex < lst_Animations.Count)
                    removeAnimationAtIndex(remIndex);
            }
        }

        public void cloneAnimationAtIndex(int curIndex)
        {
            if (lst_Animations != null && lst_Animations.Count > curIndex)
            {
                Animation newAnim = lst_Animations[curIndex].clone();
                newAnim.ID = getFreeAnimId();

                lst_Animations.Insert(curIndex + 1, newAnim);

                for (int i = curIndex + 1; i < lst_Animations.Count; i++)
                {
                    lst_Animations[i].Index += 1;
                }
            }
        }

        public void upAnimationAtIndex(int curIndex)
        {
            if (lst_Animations != null && curIndex > 0
                && lst_Animations.Count > curIndex)
            {
                Animation temp = lst_Animations[curIndex];
                lst_Animations[curIndex] = lst_Animations[curIndex - 1];
                lst_Animations[curIndex - 1] = temp;

                lst_Animations[curIndex].Index++;
                lst_Animations[curIndex - 1].Index--;
            }
        }

        public void downAnimationAtIndex(int curIndex)
        {
            if (lst_Animations != null && curIndex >= 0
                   && lst_Animations.Count - 1 > curIndex)
            {
                Animation temp = lst_Animations[curIndex];
                lst_Animations[curIndex] = lst_Animations[curIndex + 1];
                lst_Animations[curIndex + 1] = temp;

                lst_Animations[curIndex].Index--;
                lst_Animations[curIndex + 1].Index++;
            }
        }

        public void newFrameOfAnimations(int curAnim, int newFrameIndex)
        {
            if (lst_Animations != null && curAnim >= 0
                && lst_Animations.Count > curAnim)
            {
                if (lst_Animations[curAnim].lst_FramesOfAnimation == null)
                    lst_Animations[curAnim].lst_FramesOfAnimation = new List<FramesOfAnimation>();

                FramesOfAnimation newModuleOfFrames = new FramesOfAnimation()
                {
                    Index = lst_Animations[curAnim].lst_FramesOfAnimation.Count,
                    ID = lst_Frames[newFrameIndex].ID,
                    OX = 0,
                    OY = 0,
                    Time = 1
                };

                lst_Animations[curAnim].lst_FramesOfAnimation.Add(newModuleOfFrames);
                lst_Animations[curAnim].NumOfFrames++;
            }
        }

        public void deleteFrameOfAnimations(int curAnim, int curFOAmins)
        {
            if (lst_Animations != null && curAnim >= 0 && curFOAmins >= 0
                && lst_Animations.Count > curAnim
                && lst_Animations[curAnim].lst_FramesOfAnimation != null
                && lst_Animations[curAnim].lst_FramesOfAnimation.Count > curFOAmins)
            {
                lst_Animations[curAnim].lst_FramesOfAnimation.RemoveAt(curFOAmins);
                lst_Animations[curAnim].NumOfFrames--;

                if (lst_Animations[curAnim].lst_FramesOfAnimation.Count > 0)
                {
                    for (int i = 0; i < lst_Animations[curAnim].lst_FramesOfAnimation.Count; i++)
                        lst_Animations[curAnim].lst_FramesOfAnimation[i].Index = i;
                }
            }
        }

        public void cloneFrameOfAnimations(int curAnim, int curFOAmins)
        {
            if (lst_Animations != null && curAnim >= 0 && curFOAmins >= 0
                && lst_Animations.Count > curAnim
                && lst_Animations[curAnim].lst_FramesOfAnimation != null
                && lst_Animations[curAnim].lst_FramesOfAnimation.Count > curFOAmins)
            {
                FramesOfAnimation newFrameOfAnims = lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins].clone();
                lst_Animations[curAnim].lst_FramesOfAnimation.Insert(curFOAmins, newFrameOfAnims);
                lst_Animations[curAnim].NumOfFrames++;

                for (int i = curFOAmins + 1; i < lst_Animations[curAnim].lst_FramesOfAnimation.Count; i++)
                {
                    lst_Animations[curAnim].lst_FramesOfAnimation[i].Index = i;
                }
            }
        }

        public void upFrameOfAnimationsAtIndex(int curAnim, int curFOAmins)
        {
            if (lst_Animations != null && curAnim >= 0 && curFOAmins > 0
                && lst_Animations.Count > curAnim
                && lst_Animations[curAnim].lst_FramesOfAnimation != null
                && lst_Animations[curAnim].lst_FramesOfAnimation.Count > curFOAmins)
            {
                FramesOfAnimation tmp_foa = lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins];
                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins] = lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins - 1];
                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins - 1] = tmp_foa;

                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins].Index++;
                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins - 1].Index--;
            }
        }

        public void downFrameOfAnimationsAtIndex(int curAnim, int curFOAmins)
        {
            if (lst_Animations != null && curAnim >= 0 && curFOAmins >= 0
                && lst_Animations.Count > curAnim
                && lst_Animations[curAnim].lst_FramesOfAnimation != null
                && lst_Animations[curAnim].lst_FramesOfAnimation.Count - 1 > curFOAmins)
            {
                FramesOfAnimation tmp_foa = lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins];
                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins] = lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins + 1];
                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins + 1] = tmp_foa;

                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins].Index--;
                lst_Animations[curAnim].lst_FramesOfAnimation[curFOAmins + 1].Index++;
            }
        }
    }
}
